﻿
public class SolutionLCR105
{

    public int MaxAreaOfIsland(int[][] grids)//计算最大岛的面积
    {
        int m = grids.Length;
        int n = grids[0].Length;
        bool[][] visted = new bool[m][];
        for (int i = 0; i < m; i++)
        {
            visted[i] = new bool[n];
        }
        int max = 0;
        for (int i = 0; i < m; i++)
        {

            for (int j = 0; j < n; j++)
            {


                if (grids[i][j] == 1)
                {
                    int count = DFS(grids, i, j, visted);
                    max = Math.Max(max, count);


                }


            }


        }
        return max;
    }


    int DFS(int[][] grids, int i, int j, bool[][] visted)
    {
        int m = grids.Length;
        int n = grids[0].Length;



        visted[i][j] = true;
        grids[i][j] = 0;
        int left = 0;
        int right = 0;
        int top = 0;
        int bot = 0;

        if (i - 1 >= 0 && grids[i - 1][j] == 1)
        {



            top = DFS(grids, i - 1, j, visted);


        }

        if (i + 1 < m && grids[i + 1][j] == 1)
        {



            bot = DFS(grids, i + 1, j, visted);
        }

        if (j + 1 < n && grids[i][j + 1] == 1)
        {



            right = DFS(grids, i, j + 1, visted);
        }

        if (j - 1 >= 0 && grids[i][j - 1] == 1)
        {



            left = DFS(grids, i, j - 1, visted);

        }


        return left + right + top + bot + 1;
    }
}